﻿using Little.Soldier.SWIFT.Definition;

namespace Little.Soldier.SWIFT
{
    public class SwiftTagWrapper : SwiftElement
    {
        private SwiftDefinitionElement _Definition;

        internal override SwiftDefinitionElement Definition
        {
            get
            {
                return this._Definition;
            }
        }

        internal SwiftTagWrapper(SwiftElement parent, int index, SwiftDefinitionElement definition)
          : base(parent, index)
        {
            this._Definition = definition;
        }

        public override bool Equals(object obj)
        {
            if (this.GetType() != obj.GetType())
                return false;
            SwiftTag tag1 = this.GetTag();
            SwiftTag tag2 = ((SwiftElement) obj).GetTag();
            if (tag1 == null && tag2 == null)
                return true;
            if (tag1 == null || tag2 == null)
                return false;
            else
                return tag1.Equals((object) tag2);
        }

        public override string ToString()
        {
            SwiftTag tag = this.GetTag();
            if (tag == null)
            return "<null>";
            else
            return tag.Value;
        }

        internal string GetValue()
        {
            SwiftTag tag = this.GetTag();
            if (tag == null)
                return (string) null;
            else
                return tag.Value;
        }

        internal void SetValue(string tagName, string value)
        {
            SwiftTag tag = this.GetTag();
            if (value != null)
            {
                if (tag == null)
                    this.AddTag(new SwiftTag(tagName, value));
                else
                    tag.Value = value;
            }
            else
            {
                if (tag == null)
                    return;
                if (this.Definition.Mandatory)
                    tag.Value = (string) null;
                else
                    this.RemoveTag(tag);
            }
        }
    }
}
